﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NLBS.Entity;
using System.Text;
using NLBS.Data;
using System.Collections.Generic;
using NLBS.Resources;
using NLBS.Framework;
using NLBS.Data.Business;

namespace NLBS.Control
{
    public class ArticleViewHelp
    {

        public static string RenderSelectedAndLocked(Article item)
        {
            StringBuilder sb = new StringBuilder();
            Dictionary<string, string> lang=Language.Instance().getLanguage();
       
            if (item.Selected)
            {
                sb.AppendFormat("<img src=\"/styles/default/images/icon_star.gif\" alt=\"{0}\" />", lang["selected"]);
            }
            if (item.Locked || CategoryData.GetCategoryByID(item.CategoryID).Locked)
            {
                sb.AppendFormat("<img src=\"/styles/default/images/icon_lock.gif\" alt=\"{0}\" />", lang["locked"]);
            }
            return sb.ToString();
        }

        public static string RenderMode(Article item)
        {
            string mode = string.Empty;
            switch (item.Mode)
            {
                case 2:
                    mode= "草稿";
                    break;
                case 3:
                    mode = "隐藏";
                    break;
                case 4:
                    mode = "私人";
                    break;
            }
            return mode;
        }

        public static string RenderUpdateAndDelete(Article item)
        {
            StringBuilder sb = new StringBuilder();
            Dictionary<string, string> lang = Language.Instance().getLanguage();
            BlogInfo bloginfo=BlogInfo.Instance();
            if (bloginfo.CurrentUser != null)
            {
                int groupEdit = bloginfo.CurrentUser.Group.Power.Edit;
                if (groupEdit > 1 ||
                    groupEdit == 1 && (bloginfo.CurrentUser.UserID == item.AuthorID) ||
                    bloginfo.CurrentUser.UserID == item.AuthorID)
                {
                    sb.AppendFormat("<a href=\"/articleManage.aspx?act=edit&id={0}\" title=\"{1}\">", item.ArticleID, lang["edit"]);
                    sb.AppendFormat("<img src=\"/styles/default/images/icon_edit.gif\" alt=\"{0}\" /></a> | ", lang["edit"]);
                }
                int groupDelete = bloginfo.CurrentUser.Group.Power.Delete;
                if (groupDelete > 1 ||
                    groupDelete == 1 && (bloginfo.CurrentUser.UserID == item.AuthorID) ||
                    bloginfo.CurrentUser.UserID == item.AuthorID)
                {
                    sb.AppendFormat("<a href=\"/articleManage.aspx?act=delete&id={0}\" title=\"{1}\">", item.ArticleID, lang["delete"]);
                    sb.AppendFormat("<img src=\"/styles/default/images/icon_del.gif\" alt=\"{0}\" /></a> | ", lang["delete"]);
                }
            }
            return sb.ToString();
        }

        public static bool checkViewPermission(Article objArticle)
        {
            if (objArticle == null) return false;
            User user = BlogInfo.Instance().CurrentUser;
            Right power = user.Group.Power;
            bool loginIn = HttpContext.Current.User.Identity.IsAuthenticated;
            bool aa = objArticle.Mode == 1 && power.View < 1;
            bool result= !(
                      (
                        (objArticle.Mode == 1 && power.View < 1) ||
                        (objArticle.Mode == 2 && (!loginIn || power.View < 1)) ||
                        ((objArticle.Mode == 3 || objArticle.Category.Hidden) && (!loginIn || power.View < 2)) ||
                        (objArticle.Mode == 4 && (!loginIn || power.View < 3))
                      ) && objArticle.AuthorID != BlogInfo.Instance().CurrentUser.UserID
                    );
            return result;
        }
       
    }
}
